﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class DownloadEdit : PowerBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                InitControl();
                InitRequest();               
                InitDateBing();
            }
            string FileExt = LTP.Common.ConfigHelper.GetConfigString("DownloadExt");
            int FileSize = Convert.ToInt32(LTP.Common.ConfigHelper.GetConfigString("DownloadSize")) / (1024 * 1024);
            string[] tempExt = FileExt.Split(',');
            FileExt = string.Empty;
            foreach (string ext in tempExt)
            {
                FileExt += "*" + ext + ";";
            }
            FileExt = FileExt.Substring(0, FileExt.Length - 1);
            Response.Write(string.Format("<script>FileExt='{0}';FileSize={1}</script>", FileExt, FileSize));
            LblSize.Text = FileSize.ToString();

            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }
        private void InitRequest()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                lblID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ID"].Trim());
            }

            if (!string.IsNullOrEmpty(Request.QueryString["orderkey"]))
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ordertype"]))
            {
                lblOrderType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ordertype"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keytype"]))
            {
                lblKeytype.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["betime"]))
            {
                lblTimeBegin.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["edtime"]))
            {
                lblTimeOver.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
            }

            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].Trim()))
            {
                lblcurpage.Text = Request.QueryString["curpage"];
            }
            else
            {
                lblcurpage.Text = "1";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["secondname"]))
            {
                lblSecondName.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["secondname"].Trim());
                lnkStandards.Text = lblSecondName.Text + "列表";
                lnkStandards.NavigateUrl = "DownloadList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                       + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                       + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                       + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                       + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                       + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                       + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                       + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                       + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                       + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim());

            }

        }
        protected void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = (new CJJCH.BLL.T_NET_MenuSecond()).GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                   
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                   
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                    if (model.HaveChild == true)
                    {
                       
                        IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                        list = (new CJJCH.BLL.T_NET_MenuThird()).GetDrop(Convert.ToInt32(lblSecondId.Text));
                        int typeCount = list.Count;
                        if (typeCount > 0)
                        {
                            for (int i = 0; i < typeCount; i++)
                            {
                                dropType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                            }
                            list = null;
                        }
                    }
                    else
                    {
                       
                        type.Style.Add(" display", "none");
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
        }
        private void InitDateBing()
        {
           if(lblID.Text!="")
           {
               CJJCH.Model.T_NET_Download model = new Model.T_NET_Download();
               model = (new CJJCH.BLL.T_NET_Download()).GetDownloadModel(new Guid(lblID.Text));
               txtDataTitle.Text = model.DataTitle;
               txtNote.InnerHtml = model.DataNote;
               txtPlusDate.Text = DateTime.Parse(model.PlusDate.ToString()).ToString("yyyy/MM/dd"); 
               txtTrans.Text = model.OrderNum.ToString();
               hdSaveFilePath.Value = model.DataUrl;
               if(model.ThirdID!=0)
               {
                   dropType.SelectedValue = model.ThirdID.ToString();
               }
           }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (error == "")
            {
                CJJCH.Model.T_NET_Download model = new Model.T_NET_Download();
                model.ID = new Guid(lblID.Text.Trim());
                model.DataTitle = LTP.Common.StringClass.EncodeStr(txtDataTitle.Text);
                model.DataNote = LTP.Common.StringClass.RemoveSQL(txtNote.Value);
                model.DataUrl = hdSaveFilePath.Value;
                model.OrderNum = Convert.ToInt32(txtTrans.Text.Trim());
                model.PlusDate = Convert.ToDateTime(txtPlusDate.Text);
                model.BuildDate = DateTime.Now;
                model.Hits = 0;
                model.ManagerID = SessionManagerID;
                model.SecondID = Convert.ToInt32(lblSecondId.Text);
                model.FirstID = Convert.ToInt32(lblFirstID.Text);
                if (dropType.SelectedValue != "")
                {
                    model.ThirdID = Convert.ToInt32(dropType.SelectedValue);
                }
                else
                {
                    model.ThirdID = 0;
                }
                error = (new CJJCH.BLL.T_NET_Download()).EditDownload(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("修改成功！", "DownloadList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            LTP.Common.WindowClass.WindowGoHref("DownloadList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim()));
        }
        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (this.txtDataTitle.Text.Trim() == "")
            {
                error.Append("请输入标题！ \\n");
            }
            //if (this.txtNote.Value == "")
            //{
            //    error.Append("请输入内容！ \\n");
            //}
            if (this.txtPlusDate.Text.Trim() == "")
            {
                error.Append("请输入发布日期	！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(txtPlusDate.Text))
            {
                error.Append("请输入正确的日期！\\n");
            }
            if (hdSaveFilePath.Value == "")
            {
                error.Append("请上传文件!\\n");
            }
            if (this.txtTrans.Text == "")
            {
                error.Append("请输入排序编号");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtTrans.Text.Trim()))
            {
                error.Append("请输入正确的排序号！ \\n");
            }
            if (type.Style["display"] != "none")
            {
                if (this.dropType.SelectedValue == "")
                {
                    error.Append("请选择所属类型！ \\n");
                }
            }

            return error.ToString();
        }
    }
}